 

package com.zdkj.subway.stm.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.core.util.R;
import com.zdkj.common.data.mybatis.wrapper.CustomQueryWrapper;
import com.zdkj.common.log.annotation.SysLog;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.stm.entity.SwStInvoiceBalance;
import com.zdkj.subway.stm.entity.SwStInvoiceUse;
import com.zdkj.subway.stm.service.SwStInvoiceBalanceService;
import com.zdkj.subway.stm.service.SwStInvoiceUseService;
import com.zdkj.subway.stm.util.LocalDateConverter;
import com.zdkj.subway.stm.util.LocalDateTimeConverter;
import com.zdkj.subway.stm.util.SelMapUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.time.LocalDate;


/**
 * 发票结存
 *
 * @author zdkj code generator
 * @date 2020-05-25 10:42:55
 */
@RestController
@AllArgsConstructor
@RequestMapping("/swstinvoicebalance" )
@Api(value = "swstinvoicebalance", tags = "发票结存管理")
public class SwStInvoiceBalanceController {

    private final  SwStInvoiceBalanceService swStInvoiceBalanceService;
    private final SwStInvoiceUseService swStInvoiceUseService;
    /**
     * 分页查询
     * @param page 分页对象
     * @param stInvoiceBalance 发票结存
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    public R getSwStInvoiceBalancePage(Page page, SwStInvoiceBalance stInvoiceBalance) {

        return R.ok(swStInvoiceBalanceService.page(page, Wrappers.query(stInvoiceBalance).lambda()
        .eq(SwStInvoiceBalance::getStationCode, SecurityUtils.getUser().getStationCode())
        .orderByDesc(SwStInvoiceBalance::getId)
        ));
    }


    /**
     * 统计当前结存
     *  在前一天的基础上，统计今天的结存，插入记录
     * @return
     */
    @ApiOperation(value = "统计当前结存", notes = "统计当前结存")
    @GetMapping("/nowBalance" )
    public R getNowBalance() {
        SwStInvoiceUse swStInvoiceUse = new SwStInvoiceUse();
        swStInvoiceUse.setSendDate(LocalDate.now());
        swStInvoiceUse.setStationCode(SecurityUtils.getUser().getStationCode());
        return R.ok(swStInvoiceUseService.timerSave(swStInvoiceUse));
    }
    /**
     * 导出
     * @param page 分页对象
     * @param stInvoiceBalance 发票结存
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/export" )
    @PreAuthorize("@pms.hasPermission('stm_swstinvoicebalance_export')" )
    public void getSwStInvoiceBalanceExport(Page page, SwStInvoiceBalance stInvoiceBalance, HttpServletResponse response) throws IOException {
        Page a = swStInvoiceBalanceService.pageExport(page, new CustomQueryWrapper(SwStInvoiceBalance.class)
                .addDTO(stInvoiceBalance)
                .addAlis("a")
                .addDelFlag(Boolean.TRUE)
                .init());
        // 写法1
        String fileName = URLEncoder.encode("发票结存", "UTF-8");
        //"C:\\Users\\Administrator\\Desktop\\" + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        // 如果这里想使用03 则 传入excelType参数即可
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + SelMapUtils.getDatetimeNowFormatter() + ".xlsx");
        EasyExcel.write(response.getOutputStream(), SwStInvoiceBalance.class).registerConverter(new LocalDateTimeConverter())
                .registerConverter(new LocalDateConverter()).sheet("发票结存").doWrite(a.getRecords());
    }


    /**
     * 通过id查询发票结存
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(swStInvoiceBalanceService.getById(id));
    }

    /**
     * 新增发票结存
     * @param swStInvoiceBalance 发票结存
     * @return R
     */
    @ApiOperation(value = "新增发票结存", notes = "新增发票结存")
    @SysLog("新增发票结存" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('stm_swstinvoicebalance_add')" )
    public R save(@RequestBody SwStInvoiceBalance swStInvoiceBalance) {
        return R.ok(swStInvoiceBalanceService.save(swStInvoiceBalance));
    }

    /**
     * 修改发票结存
     * @param swStInvoiceBalance 发票结存
     * @return R
     */
    @ApiOperation(value = "修改发票结存", notes = "修改发票结存")
    @SysLog("修改发票结存" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('stm_swstinvoicebalance_edit')" )
    public R updateById(@RequestBody SwStInvoiceBalance swStInvoiceBalance) {
        return R.ok(swStInvoiceBalanceService.updateById(swStInvoiceBalance));
    }

    /**
     * 通过id删除发票结存
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除发票结存", notes = "通过id删除发票结存")
    @SysLog("通过id删除发票结存" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('stm_swstinvoicebalance_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(swStInvoiceBalanceService.removeById(id));
    }

}
